package com.lmh.graphdisease.controller;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.poi.word.WordUtil;
import com.lmh.graphdisease.service.OtherService;
import com.lmh.graphdisease.utils.JieBaUtil;
import com.lmh.graphdisease.utils.ResultBean;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lmh
 * @desc
 * @date 2023/3/9
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private OtherService otherService;

    /**
     *@author lmh
     *@date 2023/3/9
     *@desc 自定义健康知识文档的文本词云数据生成,接受前端的文件。并生成高频词汇map返回
     */
    @RequestMapping(path = "/fileUpload",method = RequestMethod.POST)
    public ResultBean getUploadFile(@RequestParam("multipartFiles") MultipartFile[] multipartFiles) throws IOException {
        String originalFilename = multipartFiles[0].getOriginalFilename();
        String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
        if(fileType.equals(".doc") || fileType.equals(".docx")){
            StringBuilder sb = new StringBuilder();
            if(fileType.equals(".doc")){//doc
                StringBuilder result2 = new StringBuilder();
                WordExtractor re = new WordExtractor(multipartFiles[0].getInputStream());
                result2.append(re.getText());//获取word中的文本内容
                re.close();
                sb.append(result2);
            }else{//docx
                XWPFDocument doc = new XWPFDocument(multipartFiles[0].getInputStream());
                XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
                String text = extractor.getText();//获取word中的文本内容
                extractor.close();
                sb.append(text);
            }
            List<String> sentences = JieBaUtil.getSentences(sb.toString());
            Map<String,Integer> maps = JieBaUtil.getWordsByJieBaUtil(sentences);
            List<String> result = new ArrayList<>();
            Set<String> keys = maps.keySet();
            for (String s:keys){
                result.add(s + "-*-" + maps.get(s) * 5);
            }
            return ResultBean.success(result);
        }else if(fileType.equals(".txt")){
            StringBuilder sb = new StringBuilder();
            InputStream inputStream = multipartFiles[0].getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            while (bufferedReader.ready()){
                sb.append(bufferedReader.readLine());
            }
            List<String> sentences = JieBaUtil.getSentences(sb.toString());
            Map<String,Integer> maps = JieBaUtil.getWordsByJieBaUtil(sentences);
            List<String> result = new ArrayList<>();
            Set<String> keys = maps.keySet();
            for (String s:keys){
                result.add(s + "-*-" + maps.get(s) * 5);
            }
            return ResultBean.success(result);
        }else {
            return ResultBean.fail(250,"上传文件格式无法解析!");
        }
    }


    @RequestMapping(path = "/otherNode",method = RequestMethod.POST)
    public ResultBean getOtherNode(@RequestParam(required = true) Integer identity,@RequestParam(required = true) String name){
        Map<String,Object> result = otherService.getOneOther(identity,name,10);
        if(result == null || result.isEmpty()){
            return ResultBean.fail(405,"没找到");
        }else {
            return ResultBean.success(result);
        }
    }


}
